package com.clean.cleantotal.compores.clean

import androidx.lifecycle.lifecycleScope
import com.clean.cleantotal.R
import com.clean.cleantotal.base.BaseActivity
import com.clean.cleantotal.bean.DataUtils
import com.clean.cleantotal.bean.InfoBean
import com.clean.cleantotal.bean.JunkBean
import com.clean.cleantotal.databinding.ActivityCleanScanBinding
import com.clean.cleantotal.utils.FormatterSize
import com.clean.cleantotal.utils.ScanJunkUtils
import com.clean.cleantotal.utils.Utils
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class NewCleanScanActivity : BaseActivity<ActivityCleanScanBinding>() {
    override val viewBinding: ActivityCleanScanBinding
        get() = ActivityCleanScanBinding.inflate(layoutInflater)

    private var scanJunkUtils: ScanJunkUtils? = null
    private var junkList = ArrayList<JunkBean>()

    override fun initView() {
        mBinding.back.setOnClickListener { onBackPressed() }
        val list = DataUtils.getCleanScanList()
        mBinding.recyclerView.linear().setup {
            addType<InfoBean> { R.layout.item_clean_scan_layout }
        }.models = list

        scanFile(list)
    }

    private fun scanFile(list: List<InfoBean>) {

        val cache = list[0]
        val doc = list[1]
        val temp = list[2]
        val apk = list[3]
        junkList.clear()
        scanJunkUtils = ScanJunkUtils()

        updateRecyclerView(cache, "1", 0)
        scanJunkUtils?.scanJunk(lifecycleScope, this, {
            junkList.addAll(it)
            updateRecyclerView(cache, "2", 0)
            updateRecyclerView(doc, "1", 1)
        }, {
            junkList.addAll(it)
            updateRecyclerView(doc, "2", 1)
            updateRecyclerView(temp, "1", 2)
        }, {
            junkList.addAll(it)
            updateRecyclerView(temp, "2", 2)
            updateRecyclerView(apk, "1", 3)
        }) {
            junkList.addAll(it[3].sublist)
            updateRecyclerView(apk, "2", 3)
            lifecycleScope.launch(Dispatchers.IO) {
                delay(500)
                runOnUiThread {
                    Utils.cleanGroups.clear()
                    Utils.cleanGroups.addAll(it)
                    if (!this@NewCleanScanActivity.isFinishing) {
                        openActivity(NewScanResultActivity::class.java)
                        finish()
                    }
                }
            }
        }
    }

    private fun updateRecyclerView(infoBean: InfoBean, status: String, position: Int) {
        runOnUiThread {
            infoBean.value = status
            mBinding.recyclerView.bindingAdapter.notifyItemChanged(position, false)
            val size = FormatterSize.formatFileSizeSplit(this, junkList.sumOf { it.size })
            mBinding.tvPer.text = size.first()
            mBinding.tvUnit.text = size.last()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        scanJunkUtils?.destroy()
    }
    override fun onBackPressed() {

    }
}